﻿@using KRM.Core.Library
@model IEnumerable<KRM.Core.vLoadItemDetail>

<script>
    $(document).ready(function() {
        $(".tbloaditem").dataTable({
            "iDisplayLength": 10000,
            "sScrollY": $(".tbloaditem").attr("data-height"),
            "sScrollX": "100%",
            "aaSorting": []
        });
        $('.dataTables_paginate').remove();
        
        $("#fr-MoveItemsToLoad").submit(function () {
            var serializedForm = $(this).serialize();
            if ($(".existload").is(":checked")) {
                if ($("#LoadID").val() == null) {
                    alert("No load have been selected");
                    return false;
                }
            }
            App.blockUI($('#MoveItemsToLoad'), false);
            $.ajax({
                url: "/load/moveitemstoload",
                type: "POST",
                data: serializedForm,
                success: function (data) {
                    refreshload();
                    loadcreatorloaditem(@ViewBag.Load);
                    App.unblockUI($('#MoveItemsToLoad'));
                    $("#MoveItemsToLoad").modal("hide");
                }
            });
            return false;
        });

        $("#hfwarehouseid").val(@(Model.Any() ? Model.FirstOrDefault().WarehouseID.ToString() : ""));
        

        $('input[name="loadcode"]').click(function() {
            if ($(this).val() == "Exist") {
                $("#LoadID").removeAttr("disabled");
            } else {
                $("#LoadID").attr("disabled", "disabled");
            }
        });
    });
    
    function moveitemselected() {
        var itemSelect = "";
        $(".tbloaditem tbody tr input").each(function (index, value) {
            if ($(this).is(":checked")) {
                itemSelect += $(this).val() + ",";
            }
        });
        if (itemSelect == "") {
            alert("No items have been selected");
        } else {
            $("#hfItemsSelected").val(itemSelect);
            $("#MoveItemsToLoad").modal("show");
        }
    }
    
    function removeitemsselected() {
        var itemSelect = "";
        $(".tbloaditem tbody tr input").each(function (index, value) {
            if ($(this).is(":checked")) {
                itemSelect += $(this).val() + ",";
            }
        });
        if (itemSelect == "") {
            alert("No items have been selected");
        } else {
            App.blockUI($('.dgvLoadItems'), false);
            $.ajax({                
                url: "/load/removeitemsfromloaditems",
                data:{loadid : @ViewBag.Load, items : itemSelect },
                success:function() {
                    loadcreatorloaditem(@ViewBag.Load);
                    App.unblockUI($('.dgvLoadItems'));
                }
            });
        }
    }
</script>
<table class="table table-bordered table-fix tbloaditem" data-height="200">
    <thead class="theadloaditem">
        <tr>
            <th>Selected</th>
            <th>Status</th>
            <th>Postcode</th>
            <th>OrderRef</th>
            <th>DatePlaced</th>
            <th>SpecialInstructions</th>
            <th>Product</th>
            <th>Weight</th>
            <th>Customer</th>
            <th>Qty</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
           <tr>
                <td style="text-align: center"><input type="checkbox" value="@item.OrderItemID"/></td>
                <td style="text-align: center">@Html.Raw(Utilities.LoadPlanningItemStatusImage(item.ItemStatusID ?? 0))</td>
                <td>@item.Postcode</td>
                <td>@item.OrderRef</td>
                <td>@item.DatePlaced</td>
                <td>@item.SpecialInstructions</td>
                <td>@item.Product</td>
                <td>@item.WeightKG</td>
                <td>@item.Customer</td>
                <td>@item.Qty</td>
            </tr>
        }
    </tbody>
</table>
<div id="MoveItemsToLoad" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="MoveItemsToLoad" aria-hidden="true">
    <form action="/load/MoveItemsToLoad" class="margin0" id="fr-MoveItemsToLoad" method="POST">
        <input type="hidden" name="ItemsSelected" id="hfItemsSelected"/>
        <input type="hidden" name="oldloadid" value="@ViewBag.Load"/>
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
            <h5>Create New Invoice</h5>
        </div>
        <div class="modal-body">
            <p>Move selected items to load:</p>
            <p><label><input type="radio" name="loadcode" value="Exist" class="existload" checked="checked" /> Existing Load Code</label></p>
            <div class="row-fluid">
                <div class="span1"></div>
                <div class="span6">
                    @Html.DropDownList("LoadID", null, new { @class = "input-block-level" })
                </div>
            </div>
            <p><label><input type="radio" name="loadcode" value="New"/> NewLoad</label></p>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" type="submit">Move</button>
        </div>
    </form>
</div>